package com.eyeem.recyclerviewtools.sample.widget;

import android.support.design.widget.AppBarLayout;

/**
 * 项目名称  ：RecyclerViewTools
 * 类名称    ：
 * 类描述    ：
 * 创建人    ：Aman/862032054@qq.com
 * 创建时间  ：2016/9/3 17:30
 * 修改人    ：admin
 * 修改时间  ：2016/9/3 17:30
 * 修改备注  ：
 *
 * @version V1.0
 */
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
    public enum State {
        /**
         * 展开
         */
        EXPANDED,
        /**
         * 折叠
         */
        COLLAPSED,
        /**
         * 中间
         */
        IDLE
    }

    public enum Direction {
        /**
         * 向上
         */
        UP,
        /**
         * 向下
         */
        DOWN
    }

    int mPreVerticalOffset;

    @Override
    public final void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        onOffsetChanged(verticalOffset);
        if (mPreVerticalOffset < verticalOffset) {
            onSnapDirection(Direction.DOWN);
        } else {
            onSnapDirection(Direction.UP);
        }
        mPreVerticalOffset = verticalOffset;
        if (verticalOffset == 0) {
            if (mCurrentState != State.EXPANDED) {
                onStateChanged(appBarLayout, State.EXPANDED);//修改状态标记为展开
            }
            mCurrentState = State.EXPANDED;
        } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
            if (mCurrentState != State.COLLAPSED) {
                onStateChanged(appBarLayout, State.COLLAPSED);//修改状态标记为折叠
            }
            mCurrentState = State.COLLAPSED;
        } else {
            if (mCurrentState != State.IDLE) {
                onStateChanged(appBarLayout, State.IDLE);//修改状态标记为中间
            }
            mCurrentState = State.IDLE;
        }
    }

    /**
     * 展开状态
     *
     * @param appBarLayout appBarLayout
     * @param state        state
     */
    public abstract void onStateChanged(AppBarLayout appBarLayout, State state);

    /**
     * 滑动距离
     *
     * @param verticalOffset verticalOffset
     */
    public abstract void onOffsetChanged(int verticalOffset);

    /**
     * 向上/下滑动
     *
     * @param direction direction
     */
    public abstract void onSnapDirection(Direction direction);

    private State mCurrentState = State.IDLE;
}
